﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

using Skyhook.Core.Xml;
using Skyhook.Core.Configuration.Serialize;

namespace Skyhook.Core.Configuration
{
    class DataLoader<T> : IDataLoader<T> where T : Data, new()
    {
        DataCollection<T> Collection = null;

        public DataLoader(DataCollection<T> collection)
        {
            Collection = collection;
        }

        public virtual void PreloadFromCache(XmlDocumentsCache xmlcache)
        {
            foreach (XmlDocumentWithFileName xmldoc in xmlcache)
            {
                List<T> datalist = XmlSerialize.CreateDataList<T>(xmldoc);
                
                foreach (T data in datalist)
                {
                    Collection.Add(data);
                }
            }
        }

        public virtual void LoadFromCache(XmlDocumentsCache xmlcache)
        {
            foreach (T data in Collection.Values)
            {
                XmlSerialize.DeserializeDataFromCache(data, xmlcache);
            }
        }
    }
}
